package com.atguigu.gmall.product.service.impl;
import com.atguigu.gmall.model.list.SearchAttr;
import com.atguigu.gmall.product.domain.BaseCategoryView;
import com.atguigu.gmall.product.mapper.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;

import java.util.*;

import com.atguigu.gmall.common.config.cache.aop.CacheHelper;
import com.atguigu.gmall.common.constants.RedisConst;
import com.atguigu.gmall.feign.list.ListFeignClient;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.dto.SkuValueJson;
import com.atguigu.gmall.product.service.SkuAttrValueService;
import com.atguigu.gmall.product.service.SkuImageService;
import com.atguigu.gmall.product.service.SkuSaleAttrValueService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.atguigu.gmall.product.service.SkuInfoService;
import org.redisson.api.RBloomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 *
 */
@Service
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfo>
        implements SkuInfoService {
    @Autowired
    SkuInfoMapper skuInfoMapper;
    @Autowired
    SkuImageService skuImageService;
    @Autowired
    SkuAttrValueService skuAttrValueService;
    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;
    @Autowired
    ThreadPoolExecutor poolExecutor;
    @Autowired
    RBloomFilter skuBloomFilter;
    @Autowired
    CacheHelper cacheHelper;
    @Autowired
    ListFeignClient listFeignClient;
    @Autowired
    BaseTrademarkMapper baseTrademarkMapper;
    @Autowired
    BaseCategoryViewMapper baseCategoryViewMapper;
    @Autowired
    SkuAttrValueMapper skuAttrValueMapper;
    @Transactional
    @Override
    public void saveSkuInfo(SkuInfo skuInfo) {
        skuInfoMapper.insert(skuInfo);
        //sku的自增id
        Long id = skuInfo.getId();
        //异步保存skuId到布隆过滤器中
        CompletableFuture.runAsync(()->{
            skuBloomFilter.add(id);
        },poolExecutor);
//        skuBloomFilter.add(id);
        //List<SkuImage> skuImageList;
        List<SkuImage> skuImageList = skuInfo.getSkuImageList();
        for (SkuImage skuImage : skuImageList) {
            skuImage.setSkuId(id);
        }
        skuImageService.saveBatch(skuImageList);
        // List<SkuAttrValue> skuAttrValueList;
        List<SkuAttrValue> skuAttrValueList = skuInfo.getSkuAttrValueList();
        for (SkuAttrValue skuAttrValue : skuAttrValueList) {
            skuAttrValue.setSkuId(id);
        }
        skuAttrValueService.saveBatch(skuAttrValueList);
        // List<SkuSaleAttrValue> skuSaleAttrValueList;
        List<SkuSaleAttrValue> skuSaleAttrValueList = skuInfo.getSkuSaleAttrValueList();
        for (SkuSaleAttrValue skuSaleAttrValue : skuSaleAttrValueList) {
            skuSaleAttrValue.setSkuId(id);
            skuSaleAttrValue.setSpuId(skuInfo.getSpuId());
        }
        skuSaleAttrValueService.saveBatch(skuSaleAttrValueList);

        //延时双删该skuInfo,保证缓存的一致性
        cacheHelper.deleteDelay2(RedisConst.SKUDETAIL_CACHE_KEY+id);
    }

    //上下架
    @Override
    public void upOrdown(Long skuId, int status) {
        if (status==1){
            //上架,保存goods到es
            skuInfoMapper.updateStatusById(skuId, status);
            Goods goods = getSkuInfoForSearch(skuId);
            listFeignClient.saveGoods(goods);
        }else {
            //下架,es中删除good
            skuInfoMapper.updateStatusById(skuId, status);
            listFeignClient.deleteGoods(skuId);
        }



    }
    //获取es的信息
    private Goods getSkuInfoForSearch(Long skuId) {
        Goods goods = new Goods();
        //skuInfo
        SkuInfo skuInfo = skuInfoMapper.selectById(skuId);
        goods.setId(skuId);
        goods.setDefaultImg(skuInfo.getSkuDefaultImg());
        goods.setTitle(skuInfo.getSkuName());
        goods.setPrice(skuInfo.getPrice().doubleValue());
        goods.setCreateTime(new Date());
        //baseTrademark
        BaseTrademark baseTrademark = baseTrademarkMapper.selectById(skuInfo.getTmId());
        goods.setTmId(baseTrademark.getId());
        goods.setTmName(baseTrademark.getTmName());
        goods.setTmLogoUrl(baseTrademark.getLogoUrl());
        //视图base_category_view
        QueryWrapper<BaseCategoryView> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id",skuInfo.getCategory3Id());
        BaseCategoryView baseCategoryView = baseCategoryViewMapper.selectOne(queryWrapper);
        goods.setCategory1Id(baseCategoryView.getCategory1Id());
        goods.setCategory1Name(baseCategoryView.getCategory1Name());
        goods.setCategory2Id(baseCategoryView.getCategory2Id());
        goods.setCategory2Name(baseCategoryView.getCategory2Name());
        goods.setCategory3Id(skuInfo.getCategory3Id());
        goods.setCategory3Name(baseCategoryView.getCategory3Name());
        goods.setHotScore(0L);
        //List<SearchAttr>
        // attrId平台属性Id  attrValue平台属性值名称  attrName 平台属性名
        List<SearchAttr> searchAttrList =skuAttrValueMapper.selectSearchAttrList(skuId);
        goods.setAttrs(searchAttrList);


        return goods;
    }

    @Override
    public BigDecimal getPrice(Long skuId) {
        return skuInfoMapper.getPrice(skuId);

    }

    @Override
    public List<SpuSaleAttr> getSpuSaleAttrValues(Long skuId) {
        return skuInfoMapper.getSpuSaleAttrValues(skuId);
    }

    @Override
    public Map<String, String> getSkuValueJson(Long skuId) {
        List<SkuValueJson> skuValueJson = skuInfoMapper.getSkuValueJson(skuId);
        HashMap<String, String> map = new HashMap<>();
        for (SkuValueJson valueJson : skuValueJson) {
            map.put(valueJson.getGroupValue(), valueJson.getId().toString());
        }
        return map;
    }

    @Override
    public List<Long> getIds() {
        return skuInfoMapper.getIds();
    }
}




